package com.zhongke.cotrun.common.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Map;
import java.util.Random;

public class PhoneMessageUtil {

    public static final String SIGNNAME = "科创汇";
    public static final String SIGNNAMEFOREXPERT = "中科淮北产业技术研究";
    public static final String TEMPLATECODE = "SMS_181850878";//验证码模板
    public static final String EXPERTEMPLATECODE = "SMS_183241451";//专家通知模板
    public static final String PJTEMPLATECODE = "SMS_183242192";//项目模板
    public static final String SERVICETEMPLATECODE = "SMS_184106355";//服务模板
    public static final String ACCESSKEYID = "LTAI4FgT9p5YUXHAj9Ei9hwi";//LTAIwbofXslbNoTJ
    public static final String ACCESSSECRET = "eUZUWDmB7ZizEilPij8Oqa2eDKnn9K";
    public static final String DOMAIN = "dysmsapi.aliyuncs.com";
    public static final String PRODUCT = "Dysmsapi";

    public static boolean sendMessage(String signName, String templateCode, String phoneNumber, Map<String, String> map) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);

        SendSmsRequest request = new SendSmsRequest();


        request.setMethod(MethodType.POST);

        request.setPhoneNumbers(phoneNumber);//手机号

        request.setSignName(signName);//签名

        request.setTemplateCode(templateCode);//模板
//"{\"phone\":\"13562221939\"}"

//        request.setTemplateParam(JSONObject.toJSONString(map));
        request.setTemplateParam(JSONObject.toJSONString(map));
        // 可选-上行短信扩展码(扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段)
        // request.setSmsUpExtendCode("90997");
        // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        // request.setOutId("yourOutId");

        SendSmsResponse sendSmsResponse = client.getAcsResponse(request);
        System.out.println(sendSmsResponse.getCode());
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            return true;
        }


        return false;
    }


    public static boolean sendMessageForExpert(String templateCode, String phoneNumber, Map<String, String> map) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSSECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);

        SendSmsRequest request = new SendSmsRequest();


        request.setMethod(MethodType.POST);

        request.setPhoneNumbers(phoneNumber);//手机号


        request.setTemplateCode(templateCode);//模板


        request.setTemplateParam(JSONObject.toJSONString(map));

        // 可选-上行短信扩展码(扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段)
        // request.setSmsUpExtendCode("90997");
        // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        // request.setOutId("yourOutId");

        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
//        System.out.println(sendSmsResponse.getCode());
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            return true;
        }


        return false;
    }


    //自定义方法，获取长度为size的字母数字组合的字符串
    public static String getNumber(int size) {
        String str = "0123456789";
        String number = "";
        Random r = new Random();
        for (int i = 0; i < size; i++) {
            number += str.charAt(r.nextInt(str.length()));
        }
        return number;
    }


}


